<?php

namespace app\xwam\model;

/**
 * Soft 类别模型
 *
 * @version 1.0 2018-1-31 SoChishun(14507247@qq.com) Added.
 */
class SoftCategoryModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $table = 't_content_soft_category';

    public function deleteCtegory($id = '') {
        if (!$id) {
            return xsfn_message(false, '参数无效');
        }
        // 判断是否有子项目
        $table = $this->table;
        $list = $this->query("select a.title from $table a inner join $table b on b.pid=a.id where b.pid in ($id)");
        $names = false;
        if ($list) {
            foreach ($list as $row) {
                $names .= ',' . $row['title'];
            }
        }
        if ($names) {
            return xsfn_message(false, '以下项目有子项目,请先转移子项目:' . "\n" . ltrim($names, ','));
        }
        // 判断是否有引用
        $category_ids = $this->table('t_soft')->where(array('category_id' => array('in', $id)))->getField('category_id', true);
        if ($category_ids) {
            $names = $this->where(array('id' => array('in', $category_ids)))->getField('title', true);
            return xsfn_message(false, '以下项目已被占用,无法删除:' . "\n" . implode(',', $names));
        }
        $result = $this->where(['id' => ['in', $id]])->delete();
        return xsfn_message($result);
    }

    function getChildIds($id) {
        return $this->base_getChildIds($id);
    }

    /**
     * 获取树形列表
     * @param string|array $where 默认是pid=0
     * @param string|array $field
     * @param string $order
     * @param array $childWhere
     * @return array
     * @version 2018-1-27
     */
    function selectTree($where = [], $field = 'id, title, pid, code, sort, status, create_time', $order = 'sort, id', $childWhere = []) {
        if (!$where) {
            $where = ['pid' => 0];
        }
        $list = db('content_soft_category')->where($where)->order($order)->field($field)->select();
        $aout = array();
        if ($list) {
            foreach ($list as $row) {
                $childWhereTmp = ['pid' => $row['id']];
                if ($childWhere) {
                    $childWhereTmp = array_merge($childWhere, $childWhereTmp);
                }
                // 必需有子项目且pid不等于id
                if (db('content_soft_category')->where($childWhereTmp)->count() > 0 && $row['pid'] != $row['id']) {
                    $row['children'] = $this->selectTree($childWhereTmp, $field, $order, []);
                } else {
                    $row['children'] = array();
                }
                $aout[] = $row;
            }
        }
        return $aout;
    }

    function selectTreeJson($where) {
        return $this->selectTree($where, 'id, title as text, pid');
    }

    function saveCategory() {
        $data = input('post.');
        $isAdd = empty($data['id']);
        $validate = new \app\xwam\validate\CategoryValidate();
        if (!$validate->scene($isAdd ? true : 'edit')->check($data)) {
            return xsfn_message(false, $validate->getError());
        }
        if ($isAdd) {
            unset($data['id']);
            $result = $this->isUpdate(false)->save($data);
        } else {
            if (isset($data['pid']) && $data['id'] == $data['pid']) {
                return xsfn_message(false, '父级不能是自己!'); // 2016-8-5 修正
            }
            $result = $this->isUpdate(true)->save($data);
        }
        return xsfn_message($result);
    }

    function changeStatus($id, $status) {
        $result = $this->where(array('id' => $id))->setField(array('status' => $status));
        return xsfn_message($result);
    }

    function changeSort($id, $sort) {
        $result = $this->where(array('id' => $id))->setField(array('sort' => $sort));
        return xsfn_message($result);
    }

}
